var salaryData;

function initial(){
	/**
    JSON.stringify()是将JavaScript对象转换为json字符串
  	JSON.parse(jsonstr)是将json字符串转换为javascript对象 
  	*/
	$.ajax({
		url:"http://localhost/PersonManSystem/salary/no/book.action",
//		url:"http://localhost/PersonManSystem/salary/no/book.action",
        type:"get",
        dataType:'json',
        contentType:'application/json;charset=utf-8',
        success:function(data){
        	if (data['data'] != null){    	
	        	salaryData = data;
	        	initSalaryLoadData(data);
	        	/*initSalaryLoadData(data, '2*/
        	} else {
        		alert('所有薪酬标准已经登记完毕');
        	}
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){  
          	alert("Error");  
         	alert(XMLHttpRequest.status);  
          	alert(XMLHttpRequest.readyState);  
          	alert(textStatus); 
        }
	});
	
}

/*初始化薪酬标准*/
function initSalaryLoadData(data){
	
		var salaryStandardNameSelect = $("#salarystandardname");
		$(data['data']).each(function(){
			salaryStandardNameSelect.append("<option value='" + this.salarystandardname + "'>" + this.salarystandardname + "</option>");
		});

		salarystandardnamewhenchange($("#salarystandardname").val());
}

// "salarystandardid": "10000001000A",
//             "salarystandardname": "软件开发工程师",
//             "salarysummoney": 5272,
//             "settersname": "赵子龙",
//             "bookername": null,
//             "checkername": null,
//             "booktime": null,
//             "checktime": null,
//             "changetime": null,
//             "basicsalary": 5000,
//             "trafficallowance": 500,
//             "lunchallowance": 500,
//             "commallowance": 200,
//             "endowmentins": 400,
//             "medicalins": 103,
//             "uneloymentins": 25,
//             "housingfund": 400,
//             "reviewopinion": null,
//             "changePersonname": null

function salarystandardnamewhenchange(salaryStandardName){
	$(salaryData['data']).each(function(){
		if(this.salarystandardname == salaryStandardName) {
			$("input[name = 'salarystandardid']").val(this.salarystandardid);
			$("input[name = 'settersname']").val(this.settersname);
			$("input[name = 'salarysummoney']").val(this.salarysummoney);
			$("input[name = 'basicsalary']").val(this.basicsalary);
			$("input[name = 'trafficallowance']").val(this.trafficallowance);
			$("input[name = 'lunchallowance']").val(this.lunchallowance);
			$("input[name = 'commallowance']").val(this.commallowance);
			$("input[name = 'endowmentins']").val(this.endowmentins);
			$("input[name = 'medicalins']").val(this.medicalins);
			$("input[name = 'uneloymentins']").val(this.uneloymentins);
			$("input[name = 'housingfund']").val(this.housingfund);
		}
	});

}


/*为页面加载点击添加事件*/
function loadBind(){
	/*为薪酬标准名称添加点击事件*/
	$("#salarystandardname").unbind("click");
	$("#salarystandardname").bind("click",function(){
		salarystandardnamewhenchange($("#salarystandardname").val());
	});
	
	/*为提交添加点击事件*/
	$("input[name = 'formsubmit']").unbind("click");
	$("input[name = 'formsubmit']").bind("click",function(){
		submitData();
	});
	
}



/*提交表单数据*/
function submitData(){
	var salaryInfo = {
			"salarystandardid" 		:$("input[name='salarystandardid']").val(),
			"salarystandardname" 	:$("select[name='salarystandardname']").find("option:selected").text(),
			"salarysummoney" 		:$("input[name='salarysummoney']").val(),	//已经计算好，封装就行
			"bookername" 			:$("input[name='bookername']").val(),		//由bookerid而来
			"basicsalary" 			:$("input[name='basicsalary']").val(),
			"trafficallowance" 		:$("input[name='trafficallowance']").val(),
			"lunchallowance" 		:$("input[name='lunchallowance']").val(),
			"commallowance" 		:$("input[name='commallowance']").val(),
			"endowmentins" 			:$("input[name='endowmentins']").val(),
			"medicalins" 			:$("input[name='medicalins']").val(),
			"uneloymentins" 		:$("input[name='uneloymentins']").val(),
			"housingfund" 			:$("input[name='housingfund']").val()
	};
	
	 /**
    JSON.stringify()是将JavaScript对象转换为json字符串
  	JSON.parse(jsonstr)是将json字符串转换为javascript对象 
  	*/
	$.ajax({
		url:"http://localhost/PersonManSystem/salary/update/standard.action",
//		url:"http://localhost/PersonManSystem/salary/update/standard.action",//待补充
        type:"PUT",
        dataType:'json',
        contentType:'application/json;charset=utf-8',
        data:JSON.stringify(salaryInfo),
        success:function(data){
        	if (data.msg == '成功') {
        		alert("提交成功！");
        		window.location='XCDJ.html';
        	}  
        	
        	
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){  
          	alert("Error");  
         	alert(XMLHttpRequest.status);  
          	alert(XMLHttpRequest.readyState);  
          	alert(textStatus); 
        }
	});
}